﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using System.Data.Linq;
using System.Data.SqlClient;
using Website.Logger;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;

namespace UILGestion.AlmacenAdmin
{
    public partial class NotaIngresoListForm : System.Web.UI.Page
    {

        [WebMethod(), ScriptMethod()]
        public static string GetProveedor(String prefixText, String contextKey)
        {

            ProveedorBL bol = new ProveedorBL();
            return bol.getProveedorComparativoAll(prefixText, 20, contextKey.ToInt());

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {

                    idObra.Value = (string)Request.QueryString["idObra"];
                    idAlmacen.Value = (string)Request.QueryString["idAlmacen"];

                    menuObraCtrl1.loadMenuAlmacen(int.Parse(idObra.Value), int.Parse(idAlmacen.Value)); 
                    FormatForm();


                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }


            }
        }

        //private void Find()
        //{
        //    dbGestionDataContext _db;

        //    _db = new dbGestionDataContext(Parametros.strConnString);

        //    //(DD/MM/yyyy)
        //    System.IFormatProvider MiFp =
        //                                                new System.Globalization.
        //                                                CultureInfo("es-ES", false);
        //    //valores de parametro

        //    int selectid =  int.Parse(idObra.Value);

        //    //valores de parametro

        //    //DateTime fechaInicio = new DateTime(2010, 1, 1);
        //    //DateTime fechafin = DateTime.Now;

           

        //    //if (inicioDateTime.Text.Length > 0)
        //    //{
        //    //    fechaInicio =
        //    //        System.DateTime.ParseExact(inicioDateTime.Text, "dd/MM/yyyy", MiFp);
        //    //}
        //    //if (finDateTime.Text.Length > 0)
        //    //{
        //    //    fechafin =
        //    //        System.DateTime.ParseExact(finDateTime.Text, "dd/MM/yyyy", MiFp);
        //    //}

        //    NotaIngresoBL recBol = new NotaIngresoBL();

        //    List<NotaIngresoListInfo> e = 
        //        recBol.GetNotaIngresoList(int.Parse(idObra.Value));
            
        //    entityListGridView.DataSource = e.ToList();
        //    entityListGridView.DataBind();

            

        //}

        private void FormatForm()
        {
            ShowAllComboBox();

            inicioDateTime.Text =
              DbUtil.Db.Obras.Single(p => p.idInternal == int.Parse(idObra.Value)).fechaInicio.Value.ToShortDateString();
            finDateTime.Text = DateTime.Now.ToShortDateString();
            proveedortxt.ChkVal = idObra.Value;
            //restricionesForm();
            //Find();
            //estadistica();

         
        }
        protected void ShowAllComboBox()
        {

            ListValueInfo cero = new ListValueInfo();
            cero.descripcion = "----TODOS----";
            cero.idInternal=0;
            List<ListValueInfo> lista = new List<ListValueInfo>();

            lista.Add(cero);
            lista.AddRange(new ListValueBL().GetMotivoNotaIngreso());


            motivoDDL.DataValueField = "idInternal";
            motivoDDL.DataTextField = "descripcion";
            motivoDDL.DataSource = lista;
            motivoDDL.DataBind();
          

        }


        protected void nuevoButton_Click(object sender, EventArgs e)
        {

            
            Response.Redirect("NotaIngresoInsertOPTForm.aspx?type=A&idAlmacen="+idAlmacen.Value+"&idObra=" + idObra.Value, true);
        }


        protected void obraDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
              //  Find();
                //estadistica();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void fechaInicioDateTime_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //Find();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }


        protected void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
                FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
   
    }
}